﻿@page "/admin/menu"
@using System.Linq.Expressions;
@using BlazorComponent;
@using Masa.Blazor.Presets;
@using Microsoft.AspNetCore.Authorization;
@using ThingsGateway.Admin.Application;
@using ThingsGateway.Core.Extension

@namespace ThingsGateway.Admin.Blazor
@attribute [Authorize]
@inherits BaseComponentBase
@attribute [RolePermission]
@inject UserResoures UserResoures

<MRow Style=@(IsMobile? "max-height:100%;":$"max-height: calc(100vh - {BlazorAppService.DefaultHeight}px)")>
    <MCol Md=3 Cols="12">
        <MCard Class="ma-2" Style="height:100%">
            <MCardTitle>
                <MTextField Dense Style="max-width:200px;" HideDetails=@("auto") Class="mx-2 my-1" @bind-Value="_searchName"
                            Outlined Label=@typeof(SysResource).GetDescription(nameof(SysResource.Title)) />
             </MCardTitle>
             <MTreeview Dense TItem="SysResource" TKey="long" ActiveChanged=@(
                       async a=>
                       {
                       _search.ParentId=a.FirstOrDefault();
                       await DataTableQueryAsync();
                       }
                       )
                        Items=_menuCatalog ItemText="r=>r.Title" ItemChildren="r=>r.Children"
                        Search="@_searchName" OpenOnClick
                       Activatable ItemKey=@(r=>r.Id)>
                 <PrependContent>
                     <MIcon>
                         @context.Item.Icon
                    </MIcon>
                </PrependContent>
                <LabelContent>
                    <span title=@context.Item.Title>
                        @context.Item.Title
                    </span>
                </LabelContent>
            </MTreeview>
        </MCard>
    </MCol>
    <MCol Md=9 Cols="12">

        <AppDataTable @ref="_datatable" TItem="SysResource" SearchItem="MenuPageInput"
                      AddItem="MenuAddInput" EditItem="MenuEditInput"
                      SearchModel="@_search" IsMenuOperTemplate=false
                      QueryCallAsync="QueryCallAsync" AddCallAsync="AddCallAsync" EditCallAsync="EditCallAsync" DeleteCallAsync="DeleteCallAsync"
                        IsShowDetailButton IsShowQueryButton IsShowSearchKey
                       IsShowAddButton=@UserResoures.IsHasButtonWithRole("MenuAdd")
                       IsShowDeleteButton=@UserResoures.IsHasButtonWithRole("MenuDelete")
                       IsShowEditButton=@UserResoures.IsHasButtonWithRole("MenuEdit")>

             <AddTemplate>
                 @if (_search.ParentId == 0)
                {
                    context.MenuType = MenuTypeEnum.CATALOG;
                }
                @GetRenderFragment(context)

            </AddTemplate>

            <EditTemplate>
                <MRow>
                    <MCol Cols="12">

                        <MSelect Class="" Items=@(_menuCatalog) Style="max-width:300px;"
                                 @bind-Value=@context.ParentId Label=@(context.Description(x => x.ParentId))
                                 MenuProps="@(props => { props.Auto = true; props.OffsetY = true; })"
                                 ItemText=@(u =>u.Title)
                                 ItemValue=@(u =>u.Id)
                                  Dense HideDetails=@("auto")
                                  Outlined></MSelect>
                     </MCol>

                 </MRow>

                 @GetRenderFragment(context)

            </EditTemplate>

            <ItemColOperTemplate>
                @if (context.Item.MenuType == MenuTypeEnum.MENU)
                {
                    <MButton Color="info" Text  OnClick=@(()=>ShowButtonListAsync(context.Item.Id))>
                         @AppService.I18n.T("按钮")
                     </MButton>
                }
            </ItemColOperTemplate>

            <ItemColTemplate>
                @switch (context.Header.Value)
                {
                    case nameof(context.Item.Icon):
                        <MIcon>
                            @context.Value
                        </MIcon>
                        break;
                    default:
                        @if (context.Header.CellClass?.Contains("text-truncate") == true)
                        {
                            <span title=@context.Value>
                                @context.Value
                            </span>
                        }
                        else
                        {
                            @context.Value
                        }
                        break;
                }

            </ItemColTemplate>
        </AppDataTable>

    </MCol>
</MRow>




@code {

    RenderFragment GetRenderFragment(SysResource context)
    {
        RenderFragment renderFragment =
    @<MRow>

        <MCol Cols="12">

            <MTextField Dense Outlined HideDetails="@("auto")"
                        Style="max-width:300px"
                        Label=@(context.Description(x => x.Title)) @bind-Value=@context.Title />
        </MCol>
        <MCol Cols="12">

            <MTextField Dense Outlined HideDetails="@("auto")"
                        Style="max-width:300px"
                        Label=@(context.Description(x => x.Href)) @bind-Value=@context.Href />
        </MCol>

        <MCol Cols="12">
            <MSelect Class="" Items=@(typeof(MenuTypeEnum).GetEnumList()) Style="max-width:300px;"
                     @bind-Value=@context.MenuType Label=@(context.Description(x => x.MenuType))
                     MenuProps="@(props => { props.Auto = true; props.OffsetY = true; })"
                     ItemText=@(u =>u.Description)
                     ItemValue=@(u =>(MenuTypeEnum)u.Value)
                     Dense HideDetails=@("auto")
                     Outlined></MSelect>
        </MCol>

        <MCol Cols="12">

            <MTextField Dense Outlined HideDetails="@("auto")"
                        Style="max-width:300px"
                        Label=@(context.Description(x => x.Icon)) @bind-Value=@context.Icon >
                <AppendContent>
                    <MIcon>
                        @context.Icon
                    </MIcon>
                </AppendContent>
                </MTextField>
        </MCol>

        <MCol Cols="12">
            <MRadioGroup @bind-Value=@context.Hidden Label=@(context.Description(x => x.Hidden))
                         Style="max-width:300px"
                         Row Dense HideDetails="@("auto")">
                <MRadio Label=@AppService.I18n.T("是") Value=true></MRadio>
                <MRadio Label=@AppService.I18n.T("否") Value=false></MRadio>
            </MRadioGroup>

        </MCol>

        <MCol Cols="12">
            <MSlider TValue="int" ThumbLabel="@("always")" Dense
                     Style="max-width:300px;" Class="mt-8"
                     @bind-Value=@context.SortCode Label=@(context.Description(x => x.SortCode))>
                <PrependContent>
                    <MIcon OnClick="()=>context.SortCode--">
                        mdi-minus
                    </MIcon>
                </PrependContent>

                <AppendContent>
                    <MIcon OnClick="()=>context.SortCode++">
                        mdi-plus
                    </MIcon>
                </AppendContent>
            </MSlider>
        </MCol>

    </MRow>
    ;
        return renderFragment;
    }
}
